<?php
/**
 * Created by PhpStorm.
 * User: HarrisHuang
 * Date: 17/5/13
 * Time: 15:35
 */
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Products;
use Log;

class ProductsController extends Controller
{

    public function save(Request $request, Products $products)
    {
        try {
            $products->name = $request->input('name');
            $products->logo = $request->input('logo');
            $products->linkUrl = $request->input('linkUrl');
            $products->applyNum = $request->input('applyNum');
            $products->rate = $request->input('rate');
            $products->loanRangeMin = (int)$request->input('loanRangeMin');
            $products->loanRangeMax = (int)$request->input('loanRangeMax');
            $products->succRate = $request->input('succRate');
            $products->fastTime = $request->input('fastTime');
            $products->loanLimitMin = (int)$request->input('loanLimitMin');
            $products->loanLimitMax = (int)$request->input('loanLimitMax');
            $products->tags = $request->input('tags');
            $products->proImg = $request->input('proImg');
            $products->conditions = $request->input('conditions');
            $products->material = $request->input('material');
            $products->repayType = (int)$request->input('repayType');
            $products->reditCard = (int)$request->input('reditCard');
            $products->sorts = (int)$request->input('sorts');
            $products->isHot = (int)$request->input('isHot', 0);
            $products->status = 1;
            $products->addTime = time();
            $products->save();
        } catch (\Exception $e) {
            Log::error('save products error ' . $e->getMessage());
            return $this->error();
        }
        return $this->success();
    }

    public function update(Request $request)
    {
        $id = (int)$request->input('id');
        if ($id == 0) {
            return $this->error();
        }
        $products = Products::find($id);
        $products->name = $request->input('name');
        $products->logo = $request->input('logo');
        $products->linkUrl = $request->input('linkUrl');
        $products->applyNum = $request->input('applyNum');
        $products->rate = $request->input('rate');
        $products->loanRangeMin = (int)$request->input('loanRangeMin');
        $products->loanRangeMax = (int)$request->input('loanRangeMax');
        $products->succRate = $request->input('succRate');
        $products->fastTime = (int)$request->input('fastTime');
        $products->loanLimitMin = (int)$request->input('loanLimitMin');
        $products->loanLimitMax = (int)$request->input('loanLimitMax');
        $products->tags = $request->input('tags');
        $products->proImg = $request->input('proImg');
        $products->conditions = $request->input('conditions');
        $products->material = $request->input('material');
        $products->repayType = (int)$request->input('repayType');
        $products->reditCard = (int)$request->input('reditCard');
        $products->sorts = (int)$request->input('sorts');
        $products->isHot = (int)$request->input('isHot', 0);
        $products->save();
        return $this->success();
    }

    public function delete(Request $request)
    {
        $id = (int)$request->get('id');
        $res = Products::find($id);
        $res->status = 4;
        $res->save();
        return $this->success();
    }

    public function info(Request $request)
    {
        $id = (int)$request->get('id');
        $res = Products::find($id);
        return $this->success($res);
    }

    public function mobileInfo(Request $request)
    {
        $id = (int)$request->get('id');
        $res = Products::find($id);
        return $this->success($res);
    }

    public function lists(Request $request)
    {
        $pageSize = $request->get('pageSize', 10);
        $isHot = $request->get('isHot', 0);
        $list = Products::where('status', 1);
        if ($isHot > 0) {
            $list = $list->where('isHot', $isHot);
        }
        $list = $list->orderBy('sorts', 'desc')->paginate($pageSize);
        $list = $list->toArray();
        $res = [
            'total' => $list['total'],
            'data' => $list['data'],
        ];
        return $this->success($res);
    }

    public function mobileLists(Request $request)
    {
        $pageSize = $request->get('pageSize', 10);
        $money = (int)$request->get('money', 0);
        //$limitMin = (int)$request->get('loanLimitMin', 0);
        //$limitMax = (int)$request->get('loanLimitMax', 12);
        $loanLimit = (int)$request->get('loanLimit', 0);
        $card = (int)$request->get('card', 0);
        $repayType = (int)$request->get('repayType', 0);
        $order = (int)$request->get('order', 0);
        $isHot = (int)$request->get('isHot', 0);

        $list = Products::where('status', 1);

        if ($money != '') {
            $money = explode('-', $money);
            if (count($money) == 1) {
                $list = $list->where('loanRangeMin', '<=', $money);
                $list = $list->where('loanRangeMax', '>=', $money);
            } elseif (count($money) == 2) {
                $min = $money[0];
                $max = $money[1];
                $list = $list->where(function ($query) use ($min, $max) {
                    $query->where('loanRangeMin', '<=', $min)->where('loanRangeMax', '>', $min)
                        ->orWhere(function ($query) use ($min, $max) {
                            $query->where('loanRangeMin', '>=', $min)
                                ->where('loanRangeMax', '<', $max);
                        });
                });
            }
        }
        /*
        if ($limitMax > 0) {

            $list = $list->where(function ($query) use ($limitMin, $limitMax) {
                $query->where('loanLimitMin', '<=', $limitMin)->where('loanLimitMax', '>', $limitMin)
                    ->orWhere(function ($query) use ($limitMin, $limitMax) {
                        $query->where('loanLimitMin', '>=', $limitMin)
                            ->where('loanLimitMax', '<', $limitMax);
                    });
            });

            //$list = $list->where('loanLimit', '>=', $limit);
        }
        */
        if ($loanLimit > 0) {
            $list = $list->where('loanLimitMin', '<=', $loanLimit);
            $list = $list->where('loanLimitMax', '>=', $loanLimit);
        }

        if ($card > 0) {
            $list = $list->where('reditCard', $card);
        }
        if ($repayType > 0) {
            $list = $list->where('repayType', $repayType);
        }
        if ($isHot > 0) {
            $list = $list->where('isHot', $isHot);
        }

        switch ($order) {
            case 0:
                $list = $list->orderBy('sorts', 'desc');
                break;
            case 1:
                //成功率
                $list = $list->orderBy('succRate', 'desc');
                break;
            case 2:
                //放贷速度
                $list = $list->orderBy('fastTime', 'asc');
                break;
            default:

        }
        $list = $list->paginate($pageSize);
        $list = $list->toArray();
        $res = [
            'total' => $list['total'],
            'data' => $list['data'],
        ];
        return $this->success($res);
    }


}